package com.tb.controller;

import com.tb.dubbo.*;
import com.tb.pojo.*;
import com.tb.utils.Msg;
import com.tb.utils.MsgUser;
import com.tb.utils.ReturnList;
import com.tb.utils.Utf8;
import org.omg.CORBA.ORB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderItemService orderItemService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICartItemService cartItemService;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IShopCarService shopCarService;

    @Autowired
    private IShopParamService shopParamService;
    /**
     * @api {get} /order/selectAll 用户所有订单查询 宁凯悦
     * @apiDescription 用户所有订单查询&nbsp;&nbsp;<span style="color:#D0254E"><i>[*需要用户在登陆状态下访问]</i></span>
     * @apiGroup Order API
     *
     * @apiParam {int} [page=1] 页码数
     * @apiParam {int} [rows=10] 每页数据条数
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "page": 1,
     *          "rows": 2
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess  {int} code 返回状态码:<br/> <span style="color:#1AA034">0:正常 </span>
     *                    <br/> <span style="color:red">999:未登录或者登陆超时</span>
     * @apiSuccess  {string} msg 返回错误信息
     * @apiSuccess  {Object} data 返回数据结果集
     * @apiSuccess  (data) {int} total 数据总条数
     * @apiSuccess  (data) {list} list 返回数据结果集
     * @apiSuccess  (list) {int} id 订单id
     * @apiSuccess  (list) {int} orderStatus 订单状态
     * @apiSuccess  (list) {float} orderTotalPrice 订单总价
     * @apiSuccess  (list) {User} user 用户信息
     * @apiSuccess  (list) {OrderItem} orderItem 订单详情
     * @apiSuccess  (User) {int} id 用户id
     * @apiSuccess  (User) {String} userName 用户名
     * @apiSuccess  (User) {String} realName 真实姓名
     * @apiSuccess  (OrderItem) {int} id 订单详情id
     * @apiSuccess  (OrderItem) {int} goodsNum 商品数量
     * @apiSuccess  (OrderItem) {Goods} goods 商品信息
     * @apiSuccess  (Goods) {Goods} goodsId 商品id
     * @apiSuccess  (Goods) {String} goodsName 商品名称
     * @apiSuccess  (Goods) {float} goodsPrice 商品单价
     * @apiSuccess  (Goods) {String} img 商品图片
     *
     * @apiSuccessExample {json} 正确返回值:
     *{
     *    {
     *     "code": 0,
     *     "msg": "",
     *     "data": {
     *         "total": 1,
     *         "list": [
     *             {
     *                 "id": 1,
     *                 "orderStatus": 1,
     *                 "orderTotalPrice": 5000.0,
     *                 "user": {
     *                     "id": 1,
     *                     "userName": "zhangsan",
     *                     "realName": "张三"
     *                 },
     *                 "orderItem": [
     *                     {
     *                         "id": 1,
     *                         "goodsNum": 1,
     *                         "goods": {
     *                             "goodsId": "1",
     *                             "goodsName": "iPhone10",
     *                             "goodsPrice": 5000.0,
     *                             "img": "/111"
     *                         }
     *                     }
     *                 ]
     *             }
     *         ]
     *     }
     * }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 999,
     *     "msg": "登陆超时",
     *     "data": null
     *     }
     *
     * @apiSampleRequest /order/selectAll
     */
    @GetMapping("/selectAll")
    public Msg getAllOrder(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "rows", defaultValue = "100") Integer rows, HttpServletRequest request) {
        //获取用户名
        HttpSession session = request.getSession();
        try {
            User user = (User) session.getAttribute("User");
            Map<String, Object> map = new HashMap<>();
            map.put("userName", user.getUserName());
            map.put("page", page);
            map.put("rows", rows);

            Map<String, Object> orderMap = orderService.getAllOrder(map);
            List<Order> orderList = (List<Order>) orderMap.get("olist");
            long count = (long) orderMap.get("count");

            //总条数和list数据封装
            ReturnList list = ReturnList.get(count, orderList);
            if (orderList != null) {
                return Msg.success(list);
            } else {
                return Msg.error(1, "未查到订单信息");
            }
        } catch (Exception e) {
            return Msg.error(999, "登录失效");
        }
    }

    /**
     * @api {get} /order/orderByStatusId 根据订单状态id查询用户订单详情 宁凯悦
     * @apiDescription 根据订单状态id查询用户订单详情&nbsp;&nbsp;<span style="color:#D0254E"><i>[*需要用户在登陆状态下访问]</i></span>
     * @apiGroup Order API
     *
     * @apiParam {int} [page=1] 页码数
     * @apiParam {int} [rows=10] 每页数据条数
     * @apiParam {int} [statusId=0] 订单状态id  订单状态
     *                              <br/>1，未发货
     *                              <br/>2，已发货
     *                              <br/>3，已收货
     *                              <br/>4，未付款
     *     
     * @apiParam {int} [commentsStatus=0] 评论状态 <br/>0:未评论<br/>1:已评论
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "page": 1,
     *          "rows": 2,
     *          "statusId": 1,
     *          "commentsStatus":1
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess  {int} code 返回状态码:<br/> <span style="color:#1AA034">0:正常 </span>
     *                    <br/> <span style="color:red">999:未登录或者登陆超时</span>
     * @apiSuccess  {string} msg 返回错误信息
     * @apiSuccess  {Object} data 返回数据结果集
     * @apiSuccess  (data) {int} total 数据总条数
     * @apiSuccess  (data) {list} list 返回数据结果集
     * @apiSuccess  (list) {int} id 订单id
     * @apiSuccess  (list) {int} orderStatus 订单状态
     * @apiSuccess  (list) {float} orderTotalPrice 订单总价
     * @apiSuccess  (list) {User} user 用户信息
     * @apiSuccess  (list) {OrderItem} orderItem 订单详情
     * @apiSuccess  (User) {int} id 用户id
     * @apiSuccess  (User) {String} userName 用户名
     * @apiSuccess  (User) {String} realName 真实姓名
     * @apiSuccess  (OrderItem) {int} id 订单详情id
     * @apiSuccess  (OrderItem) {int} goodsNum 商品数量
     * @apiSuccess  (OrderItem) {Goods} goods 商品信息
     * @apiSuccess  (Goods) {Goods} goodsId 商品id
     * @apiSuccess  (Goods) {String} goodsName 商品名称
     * @apiSuccess  (Goods) {float} goodsPrice 商品单价
     * @apiSuccess  (Goods) {String} img 商品图片
     *
     * @apiSuccessExample {json} 正确返回值:
     *{
     *    {
     *     "code": 0,
     *     "msg": "",
     *     "data": {
     *         "total": 1,
     *         "list": [
     *             {
     *                 "id": 1,
     *                 "orderStatus": 1,
     *                 "orderTotalPrice": 5000.0,
     *                 "user": {
     *                     "id": 1,
     *                     "userName": "zhangsan",
     *                     "realName": "张三"
     *                 },
     *                 "orderItem": [
     *                     {
     *                         "id": 1,
     *                         "goodsNum": 1,
     *                         "goods": {
     *                             "goodsId": "1",
     *                             "goodsName": "iPhone10",
     *                             "goodsPrice": 5000.0,
     *                             "img": "/111"
     *                         }
     *                     }
     *                 ]
     *             }
     *         ]
     *     }
     * }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 999,
     *     "msg": "登陆超时",
     *     "data": null
     *     }
     *
     * @apiSampleRequest /order/orderByStatusId
     */
    @GetMapping("/orderByStatusId")
    public Msg getOrderByStatusId(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "rows", defaultValue = "100") Integer rows, HttpServletRequest request, @RequestParam(value = "statusId", defaultValue = "0") Integer statusId,@RequestParam(value = "commentsStatus", defaultValue = "3") Integer commentsStatus) {
        HttpSession session = request.getSession();
        try {
            User user = (User) session.getAttribute("User");

            Map<String, Object> map = new HashMap<>();
            map.put("userName", user.getUserName());
            map.put("statusId", statusId);
            map.put("commentsStatus", commentsStatus);
            map.put("page", page);
            map.put("rows", rows);

            //取出最大行数和状态对应的list订单
            Map<String, Object> orderMap = orderService.getOrderByStatusId(map);
            if(orderMap==null){
                return Msg.error(1, "未查到订单信息");
            }
            List<Order> orderList = (List<Order>) orderMap.get("olist");
            long count = (long) orderMap.get("count");

            //总条数和list数据封装
            ReturnList list = ReturnList.get(count, orderList);
            return Msg.success(list);
        } catch (Exception e) {
            return Msg.error(999, "登录失效");
        }
    }


    /**
     * @api {get} /order/orderByOid 根据订单id查询用户订单详情 宁凯悦
     * @apiDescription 根据订单id查询用户订单详情&nbsp;&nbsp;<span style="color:#D0254E"><i>[*需要用户在登陆状态下访问]</i></span>
     * @apiGroup Order API
     *
     * @apiParam {int} [page=1] 页码数
     * @apiParam {int} [rows=10] 每页数据条数
     * @apiParam {int} [orderId=0] 订单状态id
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "page": 1,
     *          "rows": 2,
     *          "orderId": 1
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess  {int} code 返回状态码:<br/> <span style="color:#1AA034">0:正常 </span>
     *                    <br/> <span style="color:red">999:未登录或者登陆超时</span>
     * @apiSuccess  {string} msg 返回错误信息
     * @apiSuccess  {Object} data 返回数据结果集
     * @apiSuccess  (data) {int} total 数据总条数
     * @apiSuccess  (data) {list} list 返回数据结果集
     * @apiSuccess  (list) {int} id 订单id
     * @apiSuccess  (list) {String} sendAddressProvince 订单省份
     * @apiSuccess  (list) {String} sendAddressCity 订单城市
     * @apiSuccess  (list) {String} sendAddressArea 订单县区
     * @apiSuccess  (list) {String} sendAddressStreet 订单街道
     * @apiSuccess  (list) {String} recivePhone 收件人电话
     * @apiSuccess  (list) {int} orderStatus 订单状态
     * @apiSuccess  (list) {float} orderTotalPrice 订单总价
     * @apiSuccess  (list) {date} orderTime 下单时间
     * @apiSuccess  (list) {User} user 用户信息
     * @apiSuccess  (list) {OrderItem} orderItem 订单详情
     * @apiSuccess  (User) {int} id 用户id
     * @apiSuccess  (User) {String} userName 用户名
     * @apiSuccess  (User) {String} realName 真实姓名
     * @apiSuccess  (OrderItem) {int} id 订单详情id
     * @apiSuccess  (OrderItem) {int} goodsNum 商品数量
     * @apiSuccess  (OrderItem) {Goods} goods 商品信息
     * @apiSuccess  (Goods) {Goods} goodsId 商品id
     * @apiSuccess  (Goods) {String} goodsName 商品名称
     * @apiSuccess  (Goods) {float} goodsPrice 商品单价
     * @apiSuccess  (Goods) {String} img 商品图片
     *
     * @apiSuccessExample {json} 正确返回值:
     *{
     *    {
     *     "code": 0,
     *     "msg": "",
     *     "data": {
     *         "total": 1,
     *         "list": [
     *             {
     *                 "id": 1,
     *                 "orderStatus": 1,
     *                 "sendAddressProvince":"陕西",
     *                 "sendAddressCity":"西安",
     *                 "sendAddressArea":"雁塔区",
     *                 "sendAddressStreet":"南窑头",
     *                 "orderTotalPrice": 5000.0,
     *                 "orderTime":1546328149000,
     *                 "user": {
     *                     "id": 1,
     *                     "userName": "zhangsan",
     *                     "realName": "张三"
     *                 },
     *                 "orderItem": [
     *                     {
     *                         "id": 1,
     *                         "goodsNum": 1,
     *                         "goods": {
     *                             "goodsId": "1",
     *                             "goodsName": "iPhone10",
     *                             "goodsPrice": 5000.0,
     *                             "img": "/111"
     *                         }
     *                     }
     *                 ]
     *             }
     *         ]
     *     }
     * }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 999,
     *     "msg": "登陆超时",
     *     "data": null
     *     }
     *
     * @apiSampleRequest /order/orderByOid
     */
    @GetMapping("/orderByOid")
    public Msg getOrderByOrderId(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "rows", defaultValue = "100") Integer rows, HttpServletRequest request, @RequestParam(value = "orderId", defaultValue = "0") Integer orderId) {
        HttpSession session = request.getSession();
        try {
            User user = (User) session.getAttribute("User");

            Map<String, Object> map = new HashMap<>();
            map.put("userName", user.getUserName());
            map.put("oid", orderId);
            map.put("page", page);
            map.put("rows", rows);

            //取出最大行数,和详情order对象订单详情
            Map<String, Object> rmap = orderService.getOrderByOid(map);
            Long count = null;
            Order order = null;
            if (rmap.get("order") != null && rmap.get("count") != null) {
                count = (Long) rmap.get("count");
                order = (Order) rmap.get("order");
            }

            List<Order> olist = new ArrayList<>();
            olist.add(order);
            //总条数和list数据封装
            ReturnList list = ReturnList.get(count, olist);
            if (order != null) {
                return Msg.success(list);
            } else {
                return Msg.error(1, "订单未找到");
            }
        } catch (Exception e) {
            return Msg.error(999, "登录失效");
        }
    }

    /**
     * @api {get} /order/allUser 后台查询所有订单 宁凯悦
     * @apiDescription 后台查询所有订单&nbsp;&nbsp;<span style="color:#D0254E"><i>[*需要后台在登陆状态下访问]</i></span>
     * @apiGroup Order API
     *
     * @apiParam {int} [page=1] 页码数
     * @apiParam {int} [rows=10] 每页数据条数
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "page": 1,
     *          "rows": 2,
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess  {int} code 返回状态码:<br/> <span style="color:#1AA034">0:正常 </span>
     *                    <br/> <span style="color:red">1:数据有误</span>
     * @apiSuccess  {string} msg 返回错误信息
     * @apiSuccess  {Object} data 返回数据结果集
     * @apiSuccess  (data) {int} total 数据总条数
     * @apiSuccess  (data) {list} list 返回数据结果集
     * @apiSuccess  (list) {int} id 订单id
     * @apiSuccess  (list) {String} sendAddressProvince 订单省份
     * @apiSuccess  (list) {String} sendAddressCity 订单城市
     * @apiSuccess  (list) {String} sendAddressArea 订单县区
     * @apiSuccess  (list) {String} sendAddressStreet 订单街道
     * @apiSuccess  (list) {String} recivePhone 收件人电话
     * @apiSuccess  (list) {String} orderName 发起订单人姓名
     * @apiSuccess  (list) {int} orderStatus 订单状态
     * @apiSuccess  (list) {float} orderTotalPrice 订单总价
     * @apiSuccess  (list) {date} orderTime 下单时间
     *
     * @apiSuccessExample {json} 正确返回值:
     *{
     *    {
     *     "code": 0,
     *     "msg": "",
     *     "data": {
     *         "total": 1,
     *         "list": [
     *             {
     *                 "id":1,
     *                 "orderItemId":1,
     *                 "sendAddressProvince":"陕西",
     *                 "sendAddressCity":"西安",
     *                 "sendAddressArea":"雁塔区",
     *                 "sendAddressStreet":"南窑头",
     *                 "recivePhone":"13037637121",
     *                 "orderName":"张三",
     *                 "userName":"zhangsan",
     *                 "orderStatus":1,
     *                 "orderTotalPrice":5000,
     *                 "orderTime":1546328149000,
     *             }
     *         ]
     *     }
     * }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1,
     *     "msg": "数据有误",
     *     "data": null
     *     }
     *
     * @apiSampleRequest /order/allUser
     */
    @GetMapping("/allUser")
    public Msg getOrderByOrderId(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "rows", defaultValue = "10") Integer rows) {
        page = page < 1 ? 1 : page;
        rows = rows < 1 ? 1 : rows;
        try {
            Map<String, Integer> map = new HashMap<>();
            map.put("page", page);
            map.put("rows", rows);
            Map<String, Object> rmap = orderService.getAllUserOrder(map);
            Long count = (Long) rmap.get("count");
            List<Order> olist = (List<Order>) rmap.get("olist");
            //总条数和list数据封装
            ReturnList list = ReturnList.get(count, olist);
            if (olist.size() != 0) {
                return Msg.success(list);
            } else {
                return Msg.error(1, "未查到数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.error(1, "数据有误");
        }
    }

    /**
     * @api {get} /order/updateOrderStatus 订单状态 万旭刚
     * @apiDescription 订单状态改变 &nbsp;&nbsp;<span style="color:#D0254E">[*需要在用户登陆状态下请求]</span>
     * @apiGroup Order API
     *
     * @apiParam {int}  id 订单编号
     * @apiParam {int="1,2,3,4,5"}  orderStatus 订单状态码(1 未发货 2 已发货 3 已收货 4 未付款 5 已退货)
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "id":"1",
     *          "orderStatus":"1"
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess {int} [code] 返回状态码 0:正常 1:参数错误
     * @apiSuccess {string} [msg] 返回错误信息
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "",
     *     "data":null
     *    }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1(999),
     *     "msg": "接口参数错误(登陆超时)",
     *     "data": null
     * }
     *
     * @apiSampleRequest /order/updateOrderStatus
     */
    //改变订单状态  传入参数：id 订单编号  orderStatus改变状态码
    @GetMapping(value = "/updateOrderStatus")
    public Msg updateOrderStatus(Order order){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        try{
            User user = (User)session.getAttribute("User");
            String userId = user.getUserId();
            if(order.getOrderStatus()==null||order.getId()==null){
                return Msg.error(1,"接口参数错误");
            }
            int i = orderService.updateOrderStatus(order);
            if(i==1){
                return Msg.success("");
            }else{
                return Msg.error(1,"接口参数错误");
            }
        }catch (Exception e){
            return Msg.error(999,"登录超时");
        }

    }

    /**
     * @api {get} /order/updateOrderStatusByAdmin 管理员订单状态改变 万旭刚
     * @apiDescription 订单状态改变 &nbsp;&nbsp;<span style="color:#D0254E">[*需要在用户登陆状态下请求]</span>
     * @apiGroup Order API
     *
     * @apiParam {int}  id 订单编号
     * @apiParam {int="1,2,3,4,5"}  orderStatus 订单状态码(1 未发货 2 已发货 3 已收货 4 未付款 5 已退货)
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "id":"1",
     *          "orderStatus":"1"
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess {int} [code] 返回状态码 0:正常 1:参数错误
     * @apiSuccess {string} [msg] 返回错误信息
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "",
     *     "data":null
     *    }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1(999),
     *     "msg": "接口参数错误(登陆超时)",
     *     "data": null
     * }
     *
     * @apiSampleRequest /order/updateOrderStatusByAdmin
     */
    @GetMapping(value = "/updateOrderStatusByAdmin")
    public Msg updateOrderStatusByAdmin(Order order){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        try{
            User user = (User)session.getAttribute("AdminUser");
            String userId = user.getUserId();
            if(order.getOrderStatus()==null||order.getId()==null){
                return Msg.error(1,"接口参数错误");
            }
            int i = orderService.updateOrderStatus(order);
            if(i==1){
                return Msg.success("");
            }else{
                return Msg.error(1,"接口参数错误");
            }
        }catch (Exception e){
            return Msg.error(999,"登录超时");
        }

    }


    /**
     * @api {post} /order/addOrder 新增订单及购物车删除  万旭刚
     * @apiDescription 订单新增  &nbsp;&nbsp;<span style="color:#D0254E">[*需要在用户登陆状态下请求]</span>
     * @apiGroup Order API
     *
     * @apiParam {String}  recivePhone  收货人电话 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {String}  orderName  收货人姓名 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {float}  orderTotalPrice 订单总价 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {list}   list 订单商品参数列表 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam (list) {String} goodsId 商品编号 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam (list) {int} goodsNum 商品数量 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam (list) {float} goodsPrice 商品价格 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     * 	           "recivePhone":"123456",
     *             "orderName":"张三",
     *             "orderPrice":5000,
     *             "list":[
     * 	               {"goodsId":"110","goodsNum":1,"goodsPrice":5000},
     * 	               {"goodsId":"121","goodsNum":2,"goodsPrice":1000}
     *              ]
     * }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "",
     *     "data":null
     *    }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1(999),
     *     "msg": "接口参数错误(登陆超时)",
     *     "data": null
     * }
     *
     * @apiSampleRequest /order/addOrder
     */
    @PostMapping(value = "/addOrder")
    public Msg addOrders(@RequestBody OrderInfo orderInfo){
        HttpSession session = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession();
        try{
            User user =  (User)session.getAttribute("User");
            String userId = user.getUserId();
            String userName = user.getUserName();
            User u = userService.selectAddress(userId);
            Order order = new Order();
            order.setOrderStatus(2);
            Date data = new Date();
            order.setSendAddressProvince(u.getUserAddressProvince());
            order.setSendAddressCity(u.getUserAddressCity());
            order.setSendAddressArea(u.getUserAddressArea());
            order.setSendAddressStreet(u.getUserAddressStreet());
            order.setOrderName(orderInfo.getOrderName());
            order.setRecivePhone(orderInfo.getRecivePhone());
            order.setOrderTotalPrice(orderInfo.getOrderPrice());
            order.setUserName(userName);
            order.setOrderTime(data);
            int i = orderService.insertOrders(order);
            int flag = orderService.retrunId();
            OrderItem orderItem = null;
            List<GoodsInfo> list = orderInfo.getList();
            for (GoodsInfo goodsInfo: list) {
                orderItem = new OrderItem();
                orderItem.setOrderId(flag);
                orderItem.setGoodsPrice(goodsInfo.getGoodsPrice());
                orderItem.setGoodsNum(goodsInfo.getGoodsNum());
                orderItem.setGoodsId(goodsInfo.getGoodsId());
                orderItem.setCommentsStatus(0);
                orderItemService.insertOrderItem(orderItem);
                Map<String,Object> map = new HashMap<>();
                map.put("goodsId", goodsInfo.getGoodsId());
                map.put("userId", userId);
                shopParamService.deleteShopCar(map);
                shopCarService.deleteShopCars(map);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Msg.error(999,"登录超时");
        }
        return Msg.success(null);
    }

    /**
     * @api {get} /order/updateAddress 订单地址修改  万旭刚
     * @apiDescription 订单新增  &nbsp;&nbsp;<span style="color:#D0254E">[*需要在用户登陆状态下请求]</span>
     * @apiGroup Order API
     *
     * @apiParam {String}  sendAddressProvince 送货省份 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {String}  sendAddressCity  送货城市 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {String}  sendAddressArea  送货地区 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {String}  sendAddressStreet  送货街道 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {String}  orderName 收货人姓名 &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     * @apiParam {String}  recivePhone 收货人电话  &nbsp;&nbsp;<span style="color:#D0254E">[*必选字段]</span>
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "sendAddressProvince":"陕西",
     *          "sendAddressCity":"西安",
     *          "sendAddressArea":"雁塔区",
     *          "sendAddressStreet":"丈八北路",
     *          "orderName":"王五",
     *          "recivePhone":"123456789",
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess {int} code 返回状态码
     * @apiSuccess {String} msg 返回结果信息
     * @apiSuccess {object} data 返回数据结果集
     * @apiSuccess (data) {String} sendAddressProvince 省份
     * @apiSuccess (data) {String} sendAddressCity 城市
     * @apiSuccess (data) {String} sendAddressArea 地区
     * @apiSuccess (data) {String} sendAddressStreet 街道
     * @apiSuccess (data) {String} orderName 收货人姓名
     * @apiSuccess (data) {String} recivePhone 收货电话
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "",
     *     "data":{
     *         "sendAddressProvince":"陕西",
     *         "sendAddressCity":"西安",
     *         "sendAddressArea":"长安",
     *         "sendAddressStreet":"大道",
     *         "recivePhone":"12345678912",
     *         "orderName":"王五"
     *       }
     *    }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1(999),
     *     "msg": "地址修改失败(登陆超时)",
     *     "data": null
     * }
     *
     * @apiSampleRequest /order/updateAddress
     */
    @GetMapping(value = "/updateAddress")
    public Msg updateAdd(Order order){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        try{
            User user = (User) session.getAttribute("User");
            String userId = user.getUserId();
            order.setOrderName(Utf8.getString(order.getOrderName()));
            order.setSendAddressArea(Utf8.getString(order.getSendAddressArea()));
            order.setSendAddressCity(Utf8.getString(order.getSendAddressCity()));
            order.setSendAddressStreet(Utf8.getString(order.getSendAddressStreet()));
            order.setSendAddressProvince(Utf8.getString(order.getSendAddressProvince()));
            order.setRecivePhone(order.getRecivePhone());
            Map<String,Object> map = new HashMap<>();
            map.put("province",order.getSendAddressProvince());
            map.put("city",order.getSendAddressCity());
            map.put("area",order.getSendAddressArea());
            map.put("street",order.getSendAddressStreet());
            map.put("userId",userId);
            map.put("userPhone",order.getRecivePhone());
            map.put("realName",order.getOrderName());
            userService.updateAddress(map);
            return Msg.success(order);
        }catch (Exception e){
            return Msg.error(999,"登陆超时,请重新登陆");
        }
    }

    /**
     * @api {get} /order/selectUserAndShopCar 购物车和用户当前信息详情  万旭刚
     * @apiDescription 购物车和用户当前信息详情数据展示 &nbsp;&nbsp;<span style="color:#D0254E">[*需要在用户登陆状态下请求]</span>
     * @apiGroup ShopCar API
     *
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess {int} [code] 返回状态码 0:正常 1:参数错误
     * @apiSuccess {string} msg 返回错误信息
     * @apiSuccess {Object} data 返回数据结果集
     * @apiSuccess {Object} user 返回的用户详情
     * @apiSuccess (user) {int} id 数据自增标识
     * @apiSuccess (user) {String} userId 用户编号
     * @apiSuccess (user) {String} userName 用户名
     * @apiSuccess (user) {String} userPass 用户密码
     * @apiSuccess (user) {String} realName 用户真实姓名
     * @apiSuccess (user) {String} userAddressProvince 用户地址省份
     * @apiSuccess (user) {String} userAddressCity 用户地址城市
     * @apiSuccess (user) {String} userAddressArea 用户地址地区
     * @apiSuccess (user) {String} userAddressStreet 用户地址街区
     * @apiSuccess (user) {String} userPhone 用户手机号
     * @apiSuccess (data) {String} goodsId 购物车商品编号
     * @apiSuccess (data) {String} goodsName 购物车商品名称
     * @apiSuccess (data) {String} color 购物车商品颜色
     * @apiSuccess (data) {String} size 购物车商品尺码 电子产品系统等属性也用尺码表示
     * @apiSuccess (data) {float} price 购物车商品单价
     * @apiSuccess (data) {int} goodsNum 购物车商品数量
     * @apiSuccess (data) {float} totalPrice 购物车单条商品总价  结果为折扣后的价格
     * @apiSuccess {list} img 返回的商品图片集
     * @apiSuccess (img) {String} imgUrl 购物车商品图片地址
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "user":[
     *        {
     *            "id:1,
     *            "userId":"110",
     *            "userName":"zhangsan",
     *            "userPass":"122345",
     *            "realName":"张三",
     *            "userAddressProvince":"陕西",
     *            "userAddressCity":"西安",
     *            "userAddressProvince":"雁塔区",
     *            "userAddressProvince":"丈八",
     *            "userPhone":"12315615512"
     *        }
     *     ]
     *     "msg": "",
     *     "data": [
     *         {
     *             "goodsId": 1,
     *             "goodsName": "iPhone10",
     *             "color": "黑色",
     *             "size": "5.0",
     *             "price": 5000,
     *             "goodsNum": 2,
     *             "totalPrice": 8000,
     *             "img":[
     *                     {
     *                        imgUrl:"//img12.360buyimg.com/n1/s800x1026_jfs/t25570/143/2486183404/271377/9e74a22d/5be6a1c2N559da3f5.jpg!cc_800x1026.jpg"
     *                    },{
     *                       imgUrl:"https://img14.360buyimg.com/n0/jfs/t1/17309/16/2136/231494/5c19b7bfEc5b297bc/ae0039465606955d.jpg"
     *                      }
     *               ]
     *         },
     *         {
     *             "goodsId": 1,
     *             "goodsName": "iPhone10",
     *             "color": "黑色",
     *             "size": "4.3",
     *             "price": 5000,
     *             "goodsNum": 2,
     *             "totalPrice": 8000,
     *             "img":[
     *                   {
     *                     imgUrl:"//img12.360buyimg.com/n1/s800x1026_jfs/t25570/143/2486183404/271377/9e74a22d/5be6a1c2N559da3f5.jpg!cc_800x1026.jpg"
     *                   },{
     *                       imgUrl:"https://img14.360buyimg.com/n0/jfs/t1/17309/16/2136/231494/5c19b7bfEc5b297bc/ae0039465606955d.jpg"
     *                   }
     *              ]
     *         }
     *    }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1(999),
     *     "msg": "请求错误(登陆超时)",
     *     "data": null
     * }
     *
     * @apiSampleRequest /order/selectUserAndShopCar
     */
    @GetMapping(value = "/selectUserAndShopCar")
    public MsgUser selectUserAndShopCar(){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        try{
            User user = (User) session.getAttribute("User");
            String userId = user.getUserId();
            List<User> users = iUserService.selectById(userId);
            if(users==null){
                return MsgUser.error(1,"参数传递错误");
            }
            List<CartItem> list = cartItemService.selectAllCartByUser(userId);
            for (CartItem cartItem : list){
                String goodsId = goodsService.getId(cartItem.getGoodsName());
                List<Img> listPic = cartItemService.selectPicByGoodsId(goodsId);
                cartItem.setImg(listPic);
            }
            return MsgUser.success(users,list);
        }catch (Exception e){
            return MsgUser.error(999,"登录超时");
        }
    }

}
